<template>
  <div class="publish-wrap">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      class="demo-ruleForm"
      label-width="100px"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>

      <el-form-item label="内容">
        <quillEditor v-model="ruleForm.content" class="my-editor"></quillEditor>
      </el-form-item>

      <el-form-item label="封面" class="postImg">
        <el-radio-group v-model="ruleForm.cover.type">
          <el-radio :label="1">单图</el-radio>
          <el-radio :label="3">三图</el-radio>
          <el-radio :label="0">无图</el-radio>
          <el-radio :label="-1">自动</el-radio>
        </el-radio-group>

        <el-row :gutter="2" v-if="ruleForm.cover.type > 0">
          <el-col :span="6" v-for="(item,index) in ruleForm.cover.type" :key="index">
            <div class="upImg" @click="showDialog">
              <div v-if="!ruleForm.cover.images[item-1]">
                <span>点击上传图片</span>
                <i class="el-icon-picture-outline"></i>
              </div>
              <img v-else :src="ruleForm.cover.images[item-1]" alt srcset />
            </div>
          </el-col>
        </el-row>

        <el-dialog :visible.sync="dialogVisible" width="50%">
          <el-tabs v-model="ruleForm.activeName">
            <el-tab-pane label="上传图片" name="first">
              <el-upload
                class="avatar-uploader"
                action="http://ttapi.research.itcast.cn/mp/v1_0/user/images"
                :show-file-list="false"
                name="image"
                :headers="headermsg"
                :on-success="sendImg"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-tab-pane>
            <el-tab-pane label="媒体库" name="second">
              <sucai></sucai>
            </el-tab-pane>
          </el-tabs>
          <span slot="footer" class="dialog-footer">
            <el-button @click="uploadCancel">取 消</el-button>
            <el-button type="primary" @click="upoladTrue">确 定</el-button>
          </span>
        </el-dialog>
      </el-form-item>

      <el-form-item label="频道">
        <channel v-model="ruleForm.channel_id"></channel>
      </el-form-item>

      <el-form-item>
        <el-button>保存草稿</el-button>
        <el-button type="primary" @click="publish">发布</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import channel from "../../../components/channel/";
import { pushArt, pushArtical, idArtical, changeArt } from "@/api/artical.js";
import sucai from "./component/index.vue";
export default {
  data() {
    return {
      oldForm: {
        cover: {
          type: 1,
          images: []
        }
      },
      ruleForm: {
        title: "",
        content: "",
        cover: {
          type: 1,
          images: []
        },
        channel_id: "",
        activeName: "first"
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }]
      },
      dialogVisible: false,
      imageUrl: "",
      uploadurl: ""
    };
  },
  computed: {
    headermsg: function() {
      let user = window.localStorage.getItem("userInfo")
      if (user) {
        return {
          headers: {
            Authorization: `Bearer ${JSON.parse(user).token}`
          }
        };
      } else {
        return {
          headers: {
            Authorization: null
          }
        };
      }
    }
  },
  components: {
    quillEditor,
    channel,
    sucai
  },
  methods: {
    //成功长传图片之后
    sendImg(res) {
      // console.log(res);
      this.imageUrl = res.data.url;
    },
    //点击上传图片,显示弹出框
    showDialog() {
      this.dialogVisible = true;
      this.imageUrl = "";
    },
    //弹出框确认上传
    upoladTrue() {
      this.dialogVisible = false;
      this.ruleForm.cover.images.push(this.imageUrl);
    },
    //弹出框取消上传
    uploadCancel() {
      this.dialogVisible = false;
      this.imageUrl = "";
    },
    //发布功能
    async publish() {
      //修改文章内容并发布
      if (this.$route.name == "edit") {
        // console.log('修改');

        let id = this.$route.params.id;
        let res = await changeArt(id, this.ruleForm);
        // console.log(res);
      } else {
        // console.log('发布');
        //发布文章
        let res = await pushArtical(this.ruleForm);
        //  console.log(res);
        this.oldForm = this.ruleForm;
        this.$message.success("发布成功");
        this.$router.push("/artical");
      }
    },
    //根据id获取修改文章的数据
    async getData() {
      let id = this.$route.params.id;
      // console.log(id);
      let res = await idArtical(id);
      // console.log(res);
      this.ruleForm.title = res.data.data.title;
      this.ruleForm.channel_id = res.data.data.channel_id;
      this.ruleForm.content = res.data.data.content;
      this.ruleForm.cover = res.data.data.cover;

      //从修改进入页面的值保存在oldform对象中,保存初始值

      this.oldForm.title = this.ruleForm.title;
      this.oldForm.content = this.ruleForm.content;
      this.oldForm.channel_id = this.ruleForm.channel_id;
      this.oldForm.cover.type = this.ruleForm.cover.type;
      this.oldForm.cover.images = this.ruleForm.cover.images;
    }
  },
  //一进页面判断是edit还是publish页面--因为用的是同一个组件
  created() {
    if (this.$route.name == "edit") {
      // console.log('编辑页面');
      this.getData();
    } else {
      //获取初始值
      this.oldForm.title = this.ruleForm.title;
      this.oldForm.content = this.ruleForm.content;
      this.oldForm.channel_id = this.ruleForm.channel_id;
      this.oldForm.cover.type = this.ruleForm.cover.type;
      this.oldForm.cover.images = this.ruleForm.cover.images;
    }
  },
  //监听路由id的变化,更改文章
  watch: {
    "$route.params.id"() {
      if (this.$route.name == "edit") {
        this.getData();
      } else {
        this.ruleForm.title = "";
        this.ruleForm.content = "";
        this.ruleForm.channel_id = "";
        this.ruleForm.cover.type = 1;
        this.ruleForm.cover.images = "";

        this.oldForm.title = "";
        this.oldForm.content = "";
        this.oldForm.channel_id = "";
        this.oldForm.cover.type = "";
        this.oldForm.cover.images = "";
      }
    }
  },
  beforeRouteLeave(to, from, next) {
    if (
      this.oldForm.title == this.ruleForm.title &&
      this.oldForm.content == this.ruleForm.content &&
      this.oldForm.channel_id == this.ruleForm.channel_id
    ) {
      // this.oldForm.cover.type == this.ruleForm.cover.type &&
      // this.oldForm.cover.images == this.ruleForm.cover.images
      next();
    } else {
      this.$confirm("页面存在改动的内容，是否真的要离开？?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        next();
      });
    }
  }
};
</script>

<style scoped lang="less">
.publish-wrap {
  height: 100%;

  .el-form {
    height: 100%;
    overflow-x: hidden;
  }
  .my-editor {
    height: 400px;
    margin-bottom: 100px;
  }
  .postImg {
    margin-top: 30px;
  }
  .upImg {
    width: 200px;
    height: 200px;
    border: 1px solid #000;
    text-align: center;
    span {
      display: block;
      font-size: 16px;
      padding: 10px;
    }
    i {
      font-size: 80px;
    }
    img {
      width: 200px;
      height: 200px;
    }
  }
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>